package com.wnd.dell.myapplication;

import android.content.Context;
import android.widget.ImageView;
import android.widget.Toast;

import com.duowan.mobile.netroid.Listener;
import com.duowan.mobile.netroid.NetroidError;
import com.duowan.mobile.netroid.Network;
import com.duowan.mobile.netroid.Request;
import com.duowan.mobile.netroid.RequestQueue;
import com.duowan.mobile.netroid.cache.DiskCache;
import com.duowan.mobile.netroid.image.NetworkImageView;
import com.duowan.mobile.netroid.request.StringRequest;
import com.duowan.mobile.netroid.stack.HurlStack;
import com.duowan.mobile.netroid.toolbox.BasicNetwork;
import com.duowan.mobile.netroid.toolbox.FileDownloader;
import com.duowan.mobile.netroid.toolbox.ImageLoader;
import com.google.gson.Gson;

import java.io.File;

import static java.net.Proxy.Type.HTTP;

/**
 * Created by Dell on 2017/7/5.
 */

public class Netroid {
    // Netroid入口，私有该实例，提供方法对外服务。
    private static RequestQueue mRequestQueue;

    // 图片加载管理器，私有该实例，提供方法对外服务。
    private static ImageLoader mImageLoader;

    // 文件下载管理器，私有该实例，提供方法对外服务。
    private static FileDownloader mFileDownloader;

    public interface NetroidCallBack
    {
        void onSuccess(String response);
        void onFail(NetroidError error);
    }


    public class Const {
        // http parameters
        public static final int HTTP_MEMORY_CACHE_SIZE = 2 * 1024 * 1024; // 2MB
        public static final int HTTP_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
        public static final String HTTP_DISK_CACHE_DIR_NAME = "netroid";
        public static final String USER_AGENT = "netroid.cn";
    }

    private Netroid() {}

    public static RequestQueue init(Context ctx) {

        if(mRequestQueue ==null) {
            // 创建Netroid主类，指定硬盘缓存方案
            Network network = new BasicNetwork(new HurlStack(Const.USER_AGENT, null), "UTF-8");
            mRequestQueue = new RequestQueue(network, 4, new DiskCache(
                    new File(ctx.getCacheDir(), Const.HTTP_DISK_CACHE_DIR_NAME), Const.HTTP_DISK_CACHE_SIZE));

            mRequestQueue.start();
            return mRequestQueue;
        }else
        {
            return mRequestQueue;
        }

    }

    public static void getWeather(final Netroid.NetroidCallBack netroidCallBack) {
        String url = "http://v.juhe.cn/weather/index?format=2&cityname=苏州&key=2ef72046b221faa4e1c2ece645351f5a";
        StringRequest request = new StringRequest(Request.Method.GET, url, new Listener<String>() {
            @Override
            public void onSuccess(String response) {

                netroidCallBack.onSuccess(response);


            }

            @Override
            public void onError(NetroidError error) {
                netroidCallBack.onFail(error);
            }
        });

        request.addHeader("Accept-Encoding", "gzip, deflate");
        mRequestQueue.add(request);
    }


    public static void getTrain(String trainName,String key,final Netroid.NetroidCallBack netroidCallBack)
    {
        String url = "http://apis.juhe.cn/train/s?name="+trainName+"&key="+key;
        StringRequest request = new StringRequest(Request.Method.GET, url, new Listener<String>() {
            @Override
            public void onSuccess(String response) {

                netroidCallBack.onSuccess(response);


            }

            @Override
            public void onError(NetroidError error) {
                netroidCallBack.onFail(error);
            }
        });

        request.addHeader("Accept-Encoding", "gzip, deflate");
        mRequestQueue.add(request);
    }

    public static void getFood(final Netroid.NetroidCallBack netroidCallBack){
        String url = "http://apis.juhe.cn/cook/query?key=491fff74ecc124185cd7242b7a691742&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=3";
        StringRequest request = new StringRequest(Request.Method.GET, url, new Listener<String>() {
            @Override
            public void onSuccess(String response) {

                netroidCallBack.onSuccess(response);


            }

            @Override
            public void onError(NetroidError error) {
                netroidCallBack.onFail(error);
            }
        });

        request.addHeader("Accept-Encoding", "gzip, deflate");
        mRequestQueue.add(request);
    }

    public static void getbook(final Netroid.NetroidCallBack netroidCallBack){
        String url = "http://japi.juhe.cn/comic/category?key=9947a11f3ca1abdf1925618b6baab736";
        StringRequest request = new StringRequest(Request.Method.GET, url, new Listener<String>() {
            @Override
            public void onSuccess(String response) {

                netroidCallBack.onSuccess(response);


            }

            @Override
            public void onError(NetroidError error) {
                netroidCallBack.onFail(error);
            }
        });

        request.addHeader("Accept-Encoding", "gzip, deflate");
        mRequestQueue.add(request);
    }



}



